package com.wendy.rabbit.consumer;

import com.wendy.rabbit.Constants.TestMqConstants;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.Map;
@Component
public class RabbitConsumer {


    //支持自动声明绑定，声明之后自动监听队列的队列，此时@RabbitListener注解的queue和bindings不能同时指定，否则报错
    @RabbitListener(bindings =
            {@QueueBinding(
             value = @Queue(value = TestMqConstants.Queue.queue, durable = Exchange.TRUE),
            exchange = @Exchange(value = TestMqConstants.Exchange.exchangeName, durable = Exchange.TRUE,
                    type= ExchangeTypes.FANOUT), key = "test")
            })
    @RabbitHandler
    public void receive(@Payload Message message, @Headers Map<String, Object> headers,String value) {
        long tag = message.getMessageProperties().getDeliveryTag();
        System.out.println("FANOUT队列1:{"+tag+"}"+value);
    }

    //支持自动声明绑定，声明之后自动监听队列的队列，此时@RabbitListener注解的queue和bindings不能同时指定，否则报错
    @RabbitListener(bindings =
            {@QueueBinding(
                    value = @Queue(value = TestMqConstants.Queue.queue2, durable = Exchange.TRUE),
                    exchange = @Exchange(value = TestMqConstants.Exchange.exchangeName, durable = Exchange.TRUE,
                            type= ExchangeTypes.FANOUT), key = "test")
            })
    @RabbitHandler
    public void receive2(@Payload Message message, @Headers Map<String, Object> headers,String value) {
        long tag = message.getMessageProperties().getDeliveryTag();
        System.out.println("FANOUT队列2:{"+tag+"}"+value );
    }


    @RabbitListener(bindings =
            {@QueueBinding(
                    value = @Queue(value = TestMqConstants.Queue.queue3, durable = Exchange.TRUE),
                    exchange = @Exchange(value = TestMqConstants.Exchange.topicExchangeName, durable = Exchange.TRUE,
                            type= ExchangeTypes.TOPIC), key = "#.test.#")
            })
    @RabbitHandler
    public void receive3(@Payload Message message, @Headers Map<String, Object> headers,String value) {
        long tag = message.getMessageProperties().getDeliveryTag();
        System.out.println("Topic1 队列:{"+tag+"}"+value);
    }

    @RabbitListener(bindings =
            {@QueueBinding(
                    value = @Queue(value = TestMqConstants.Queue.queue3, durable = Exchange.TRUE),
                    exchange = @Exchange(value = TestMqConstants.Exchange.topicExchangeName, durable = Exchange.TRUE,
                            type= ExchangeTypes.TOPIC), key = "#.test.#")
            })
    @RabbitHandler
    public void receive4(@Payload Message message, @Headers Map<String, Object> headers,String value) {
        long tag = message.getMessageProperties().getDeliveryTag();
        System.out.println("Topic2 队列:{"+tag+"}"+value);
    }
    @RabbitListener(bindings =
            {@QueueBinding(
                    value = @Queue(value = TestMqConstants.Queue.queue4, durable = Exchange.TRUE),
                    exchange = @Exchange(value = TestMqConstants.Exchange.topicExchangeName, durable = Exchange.TRUE,
                            type= ExchangeTypes.TOPIC), key = "#.test.#")
            })
    @RabbitHandler
    public void receive5(@Payload Message message, @Headers Map<String, Object> headers,String value) {
        long tag = message.getMessageProperties().getDeliveryTag();
        System.out.println("Topic3 队列:{"+tag+"}"+value);
    }

}
